package org.internna.ossmoney.mvc;

import java.util.Date;
import java.util.Locale;
import java.util.TreeSet;
import java.util.Currency;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import org.internna.ossmoney.model.Bill;
import org.internna.ossmoney.model.Payee;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.services.AccountService;
import org.internna.ossmoney.model.security.UserDetails;
import org.internna.ossmoney.model.Bill.RECURRING_INTERVAL;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.propertyeditors.CustomDateEditor;

@Controller
@RequestMapping("/financial/bills")
public final class BillsController {

	@Autowired private AccountService accountService;
	@Autowired private DashboardController dashboard;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @RequestMapping
    public String index(ModelMap modelMap) {
    	UserDetails user = UserDetails.findCurrentUser();
    	modelMap.addAttribute("bills", Bill.findByOwner(user));
        return "bills/index";
    }

    @RequestMapping("/create")
    public String createForm(HttpServletRequest request, ModelMap modelMap) {
    	UserDetails user = UserDetails.findCurrentUser();
    	modelMap.addAttribute("payees", Payee.findByOwner(user));
    	modelMap.addAttribute("categories", new TreeSet<Subcategory>(Subcategory.findExpenseCategories(user)));
    	return "bills/create";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createBill(RECURRING_INTERVAL period, String description, Double amount, Date due, Long payee, String locale, Long subcategory, ModelMap modelMap) {
    	createBill(period, description, amount, due, payee, locale.split("_"), subcategory);
    	return dashboard.index(modelMap);
    }

    protected void createBill(RECURRING_INTERVAL period, String description, Double amount, Date due, Long payee, String[] locale, Long subcategory) {
    	Bill bill = new Bill();
    	bill.setPeriod(period);
    	bill.setAmount(amount);
    	bill.setLastTrigger(due);
    	bill.setDescription(description);
    	bill.setPayee(Payee.findPayee(payee));
    	bill.setOwner(UserDetails.findCurrentUser());
    	bill.setCurrency(new Locale(locale[0], locale[1], ""));
    	bill.setCategory(Subcategory.findSubcategory(subcategory));
    	bill.persist();
    }

    @RequestMapping("/pay/{id}")
    public String pay(@PathVariable Long id, ModelMap modelMap) {
    	UserDetails user = UserDetails.findCurrentUser();
    	Bill bill = Bill.findBill(id);
    	if (bill.belongsTo(user)) {
	    	modelMap.addAttribute("bill", bill);
	    	modelMap.addAttribute("origin", user.getBankAccounts(bill.getCurrency()));
	    	modelMap.addAttribute("currency", Currency.getInstance(bill.getCurrency()).getCurrencyCode());
    	}
    	return "bills/pay";
    }

    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public String pay(Long id, Long origin, Double amount, Date operationDate, ModelMap modelMap) {
    	UserDetails user = UserDetails.findCurrentUser();
    	Bill bill = Bill.findBill(id);
    	if (bill.belongsTo(user)) {
    		accountService.payBill(user, bill, origin, amount, operationDate);
    	}
    	return dashboard.index(modelMap);
    }

}
